package com.my.chapter15;

import java.util.HashMap;
import java.util.Map;

public class practice21 {
    public static void main(String[] args) {
        ClassTypeCapture ctt = new ClassTypeCapture();
        ctt.addType("Tuple",Tuple.class);
        ctt.addType("CountedObject",CountedObject.class);
        System.out.println(ctt.createNew("Tuple").getClass());
        System.out.println(ctt.createNew("CountedObject").getClass());
        ctt.createNew("Product");
    }
}

class ClassTypeCapture {
    Map<String,Class<?>> types =
            new HashMap<String,Class<?>>();
    public void addType(String typename, Class<?> kind) {
        types.put(typename, kind);
    }
    public Object createNew(String typename) {
        Class<?> cl = types.get(typename);
        try {
            return cl.newInstance();
        } catch(NullPointerException e) {
            System.out.println("Not a registered typename: " + typename);
        } catch(Exception e) {
            System.out.println(e.toString());
        }
        return null;
    }
}
